import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const routes = [
  {
    path: '/admin/login',
    name: 'Login',
    component: () => import('@/views/admin/login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/admin',
    name: 'Layout',
    component: () => import('@/views/layout.vue'),
    meta: { requiresAuth: true },
    redirect: '/admin/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/admin/dashboard/index.vue')
      },
      // 员工管理
      {
        path: 'employee',
        name: 'Employee',
        component: () => import('@/views/admin/employee/index.vue')
      },
      // 药材分类管理
      {
        path: 'herb-category',
        name: 'HerbCategory',
        component: () => import('@/views/admin/herb-category/index.vue')
      },
      {
        path: 'herb-category/add',
        name: 'HerbCategoryAdd',
        component: () => import('@/views/admin/herb-category/edit.vue')
      },
      {
        path: 'herb-category/edit/:id',
        name: 'HerbCategoryEdit',
        component: () => import('@/views/admin/herb-category/edit.vue')
      },
      // 中药材管理
      {
        path: 'chinese-herb',
        name: 'ChineseHerb',
        component: () => import('@/views/admin/chinese-herb/index.vue')
      },
      {
        path: 'chinese-herb/add',
        name: 'ChineseHerbAdd',
        component: () => import('@/views/admin/chinese-herb/edit.vue')
      },
      {
        path: 'chinese-herb/edit/:herbId',
        name: 'ChineseHerbEdit',
        component: () => import('@/views/admin/chinese-herb/edit.vue')
      },
      // 药材库存管理
      {
        path: 'herb-inventory',
        name: 'HerbInventory',
        component: () => import('@/views/admin/herb-inventory/index.vue')
      },
      {
        path: 'herb-inventory/add',
        name: 'HerbInventoryAdd',
        component: () => import('@/views/admin/herb-inventory/edit.vue')
      },
      {
        path: 'herb-inventory/edit/:id',
        name: 'HerbInventoryEdit',
        component: () => import('@/views/admin/herb-inventory/edit.vue')
      },
      {
        path: 'herb-inventory/warning',
        name: 'InventoryWarning',
        component: () => import('@/views/admin/herb-inventory/warning.vue')
      },
      {
        path: 'herb-inventory/expiring',
        name: 'InventoryExpiring',
        component: () => import('@/views/admin/herb-inventory/expiring.vue')
      },
        // 中医药问答小助手
        {
          path: 'chat-assistant',
          name: 'ChatAssistant',
          component: () => import('@/views/admin/chat-assistant/index.vue')
        },
    ]
  },
  {
    path: '/',
    redirect: '/admin/dashboard'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()

  if (to.meta.requiresAuth && !authStore.isLoggedIn) {
    next('/admin/login')
  } else if (to.path === '/admin/login' && authStore.isLoggedIn) {
    next('/admin/dashboard')
  } else {
    next()
  }
})

export default router
